package io.hepu.robotize.enums;

/**
 * 角色类型
 */
public enum RoleType {
    SUPERVISOR(15, "supervisor", "超级管理员", "全局管理员具有后台管理最高权限") {
        @Override
        public boolean isSupervisor() {
            return true;
        }
    },
    MANAGER(7, "manager", "后台管理员", "管理员具有后台管理全部权限") {
        @Override
        public boolean isManager() {
            return true;
        }
    },
    OPERATOR(3, "operator", "后台操作员", "操作员具有后台业务部分权限") {
        @Override
        public boolean isOperator() {
            return true;
        }
    },
    MEMBER(1, "member", "普通用户", "普通用户具有平台业务访问权限") {
        @Override
        public boolean isMember() {
            return true;
        }
    },
    GUEST(0, "guest", "访客人员", "访客具有平台开放业务访问权限") {
        @Override
        public boolean isGuest() {
            return true;
        }
    };

    public boolean isSupervisor() {
        return false;
    }

    public boolean isManager() {
        return false;
    }

    public boolean isOperator() {
        return false;
    }

    public boolean isMember() {
        return false;
    }

    public boolean isGuest() {
        return false;
    }

    // role level
    private final int privilege;
    private final String name;
    private final String alias;
    private final String remark;

    RoleType(int privilege, String name, String alias, String remark) {
        this.privilege = privilege;
        this.name = name;
        this.alias = alias;
        this.remark = remark;
    }

    // 计算权限量
    public static int privs(String... roleNames) {
        int priority = 0;
        for (String roleName : roleNames) {
            RoleType type = fromName(roleName);
            priority += type.getPrivilege();
        }
        return priority;
    }

    public static RoleType fromName(String name) {
        for (RoleType type : values()) {
            if (name.equalsIgnoreCase(type.name())) {
                return type;
            }
        }
        throw new IllegalArgumentException("No matching constant for [" + name + "]");
    }

    /*-------------------------------------
    |              A C C E S S              |
    =======================================*/
    public int getPrivilege() {
        return privilege;
    }

    public String getName() {
        return name;
    }

    public String getAlias() {
        return alias;
    }

    public String getRemark() {
        return remark;
    }
}
